package com.ignou.mca.exam;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ignou.mca.beans.QuestionsBean;
import com.ignou.mca.beans.UserVO;
import com.ignou.mca.datasource.DatabaseOperations;

/**
 * Servlet implementation class Questions
 */
public class Questions extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Questions() 
    {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		HttpSession session = request.getSession();
		UserVO uvo = (UserVO)session.getAttribute("UserVO");
		String command = request.getParameter("CMD");
		String questionId = request.getParameter("questionId");
		String subjectId = request.getParameter("subjectId");
		String questionType = request.getParameter("questionType");
		String question = request.getParameter("question");
		String options = request.getParameter("options");
		String correctAnswer = request.getParameter("correctAnswer");
		String marks = request.getParameter("marks");
		
		QuestionsBean qb = new QuestionsBean();
		qb.setSubjectId(Integer.parseInt(subjectId));
		qb.setQuestionId(Integer.parseInt(questionId));
		qb.setQuestiontype(questionType);
		qb.setQuestion(question);
		StringTokenizer st = new StringTokenizer(options,"|");
		ArrayList<String> optElements = new ArrayList<String>();
		
		while(st.hasMoreTokens())
		{
			optElements.add(st.nextToken());
		}
		
		qb.setOptions(optElements);
		qb.setCorrectAnswer(correctAnswer);
		qb.setMarks(Integer.parseInt(marks));
		qb.setCreatedBy(uvo.getUserName());
		qb.setUpdatedBy(uvo.getUserName());
		
		
		DatabaseOperations dbOperations = new DatabaseOperations();
		ServletContext ctx = getServletContext();
		
		if(command.equalsIgnoreCase("update"))
		{
			Boolean isUpdateSuccess = false;
			isUpdateSuccess = dbOperations.updateQuestion(qb);
			if(isUpdateSuccess)
			{
				ctx.getRequestDispatcher("/Subjects.jsp").forward(request, response);
			}else
			{
				session.setAttribute("ErrorMessage", "Unable to to contact server at this time. <br>Please try again after sometime.");
				response.sendRedirect("./ErrorDisplay.jsp");
			}
		}
		else
		{
			Boolean isAddSuccess = false;
			isAddSuccess = dbOperations.addQuestion(qb);
			if(isAddSuccess)
			{
				ctx.getRequestDispatcher("/Questions.jsp?subject="+subjectId).forward(request, response);
			}else
			{
				session.setAttribute("ErrorMessage", "Unable to to contact server at this time. <br>Please try again after sometime.");
				ctx.getRequestDispatcher("/ErrorDisplay.jsp").forward(request, response);
			}
		}
	}

}
